package palms.messages;

import java.io.IOException;
import java.net.Socket;

import javax.crypto.SealedObject;

import palms.exceptions.InvalidClassMessage;
import palms.exceptions.InvalidTicketMessage;
import palms.exceptions.InvalidUserMessage;
import palms.security.PalmsSecurityManager;

public class SIResponse extends PMessage{

	private static final long serialVersionUID = 8849306088572525518L;
	
	private SealedObject result;
	
	public SIResponse(SealedObject result) {
		this.result = result;
	}
	public SIResponse() {
	}

	public void setResult(SealedObject result) {
		this.result = result;
	}

	public SealedObject getResult() {
		return result;
	}
	
	public void sendTroughSocket(Socket s) throws IOException{
		PalmsSecurityManager.sendObjectTroughtSocket(s, new SIResponse());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getResult());
	}
	
	public static SIResponse receiveTroughSocket(Socket s) throws IOException, ClassNotFoundException, InvalidClassMessage, InvalidUserMessage, InvalidTicketMessage{
		Object o = PalmsSecurityManager.receiveObjectTroughSocket(s);
		if(!(o instanceof SIResponse))
			PalmsSecurityManager.throwCommException(o);
		else{
			SIResponse si = (SIResponse)o;
			SealedObject res = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			si.setResult(res);
			return si;
		}
		return null;
	}
	
}
